//
//  FAQViewController.m
//  JimiPay
//
//  Created by yull on 16/1/7.
//  Copyright (c) 2016年 com.shengyun. All rights reserved.
//

#import "FAQViewController.h"
#import "FAQCell.h"

#import "SVPullToRefresh.h"

@interface FAQViewController ()

@property (nonatomic, strong) NSMutableArray *faqArray;
@property (nonatomic, assign) NSInteger start;
@property (nonatomic, strong) NSString *lastId;

@end

@implementation FAQViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = @"常见问题";
    [self setupCustomBackItem];
    
    self.faqArray = [NSMutableArray array];
    [self.tableView registerNib:[UINib nibWithNibName:@"FAQCell" bundle:nil] forCellReuseIdentifier:@"FAQCell"];
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    __weak typeof(self) wself = self;
    
    [self.tableView addPullToRefreshWithActionHandler:^{
        wself.start = 0;
        wself.lastId = nil;
        [wself loadData];
    }];
    
    [self.tableView addInfiniteScrollingWithActionHandler:^{
        [wself loadData];
    }];
    
    [self loadData];
}

- (void)loadData {
    
    [TDHttpEngine requestFor_GET_FAQ_WithCustId:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID]
                                     custMobile:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE]
                                          start:self.start
                                       pageSize:10
                                       complete:^(BOOL succeed, NSString *msg, id reqBody) {
                                           
                                           [self.tableView.pullToRefreshView stopAnimating];
                                           [self.tableView.infiniteScrollingView stopAnimating];
                                           
                                           if (succeed) {
                                               NSArray *list = [reqBody objectForKey:@"LIST"];
                                               
                                               if (self.start == 0) {
                                                   [self.faqArray removeAllObjects];
                                               }
                                               
                                               [self.faqArray addObjectsFromArray:list];
                                               
                                               [self.tableView reloadData];
                                               self.start += list.count;
                                               
                                               if (list.count < 10) {
                                                   self.tableView.showsInfiniteScrolling = NO;
                                                   if (self.faqArray.count == 0) {
                                                       self.blankView.hidden = NO;
                                                   }
                                               }
                                           } else {
                                               [self.view makeToast:msg duration:2.0f position:@"center"];
                                           }
    }];
}

- (void)loadContent:(FAQCell *)cell indexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dic = self.faqArray[indexPath.row];
    
    cell.titleL.text = dic[@"faqTitle"];
    
//    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[dic[@"faqBody"] dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
//    cell.contentL.attributedText = attrStr;
    cell.contentL.text = dic[@"faqBody"];
}

#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 91;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static FAQCell *cell;
    if (!cell) {
        cell = [tableView dequeueReusableCellWithIdentifier:@"FAQCell"];
    }
    
    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];
    
    [self loadContent:cell indexPath:indexPath];
    
    CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    return size.height + 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.faqArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FAQCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FAQCell"];
    
    [self loadContent:cell indexPath:indexPath];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
}

@end
